package org.aristotle.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.user.model.dto.RoleDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
// 本模块
import org.aristotle.user.model.entity.Role;
import org.aristotle.user.model.vo.RoleVO;
import org.aristotle.user.mapper.RoleMapper;
import org.aristotle.user.service.IRoleService;

// 一对多(本类是多)
// 多对一(本类是多)
// 多对多关系
import org.aristotle.user.model.entity.UserRole;
import org.aristotle.user.service.IUserRoleService;
import org.aristotle.user.model.entity.RolePermission;
import org.aristotle.user.service.IRolePermissionService;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    // 多对一(本类是多)

    // 一对多(本类是一)

    // 多对多关系中间表
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IRolePermissionService rolePermissionService;

    @Override
    public Page<RoleVO> queryPageList(
        Long id,
        String identifier,
        String name,
        Integer current,
        Integer size
    ) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper();

        if (id != null) {
            queryWrapper.eq("id",id);
        }

            if (!StringUtils.isBlank(identifier)) {
                queryWrapper.like("identifier",identifier);
            }
            if (!StringUtils.isBlank(name)) {
                queryWrapper.like("name",name);
            }


        Page<Role> page = this.page(new Page<Role>(current,size), queryWrapper);

        // 转为VO
        Page<RoleVO> result = AristotleBeanUtils.transitionBean(page, Page<RoleVO>::new);
        result.setRecords(AristotleBeanUtils.copyListProperties(page.getRecords(),RoleVO::new));

        result.getRecords().forEach(roleVO -> {
            // 多对一(本类是多)
        });
        return result;
    }

    @Override
    public RoleVO getById(Long id) {
        Role role = this.baseMapper.selectById(id);
        RoleVO roleVO = AristotleBeanUtils.transitionBean(role, RoleVO::new);
        return roleVO;
    }

    @Override
    @Transactional
    public Boolean removeById(Long id) {
        /*删除中间表*/
        QueryWrapper<UserRole> queryWrapperUserRole = new QueryWrapper<>();
        queryWrapperUserRole.eq("role_id",id);
        userRoleService.remove(queryWrapperUserRole);
        QueryWrapper<RolePermission> queryWrapperRolePermission = new QueryWrapper<>();
        queryWrapperRolePermission.eq("role_id",id);
        rolePermissionService.remove(queryWrapperRolePermission);
        /*一对多(本类是一)*/

        return super.removeById(id);
    }

    @Override
    public RoleDTO getDTOById(Long roleId) {
        Role role = super.getById(roleId);
        RoleDTO result = rolePOToDTO(role);
        result.setPermissionList(this.rolePermissionService.getPermissionDTOByRoleId(result.getId()));
        return result;
    }

    @Override
    public Role getPoById(Long id) {
        return super.getById(id);
    }

    private RoleDTO rolePOToDTO(Role role) {
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(role,roleDTO);
        return roleDTO;
    }

}
